package com.wx.io.c10k;

import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.nio.charset.StandardCharsets;
import java.util.LinkedList;

/**
 * 1W次请求
 * @author wangxun
 * @version 1.0
 * @description: 1W次请求
 * @date 2021/1/4 12:57 下午
 */
public class C10KClient {
    public static void main(String[] args) {
        LinkedList<SocketChannel> clients = new LinkedList<>();
        InetSocketAddress serverAddr = new InetSocketAddress("127.0.0.1", 9999);

        //端口号的问题：65535
        //  windows
        for (int i = 10000; i < 65000; i++) {
            try {
                SocketChannel client1 = SocketChannel.open();
                client1.configureBlocking(false);

                SocketChannel client2 = SocketChannel.open();

                /*
                linux中你看到的连接就是：
                client...port: 10508
                client...port: 10508
                 */

                client1.bind(new InetSocketAddress("127.0.0.1", i));
                //  192.168.150.1：10000   192.168.150.11：9090
                client1.connect(serverAddr);
                ByteBuffer buffer = ByteBuffer.allocate(1024);
                client1.write(buffer.put("123".getBytes()));

                clients.add(client1);


            } catch (Exception e) {
                e.printStackTrace();
            }


        }
        System.out.println("clients "+ clients.size());

        try {
            System.in.read();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
